﻿using MxWeiXinPF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MxWeiXinPF.Web.shop
{
    public partial class userratepoints : MxWeiXinPF.Web.UI.UserPage
    {
        private string action = MXEnums.ActionEnum.Add.ToString();
        public string carturl = "";
        public string historyRate = "0";
        public string indexurl = "";
        public string thisRate = "0";
        protected int uid = 0;
        protected MxWeiXinPF.Model.users user_info;
        public string userinfourl = "";
        public string yjsRate = "0";
        public string categoryurl = "";

        private void BindCode(int id)
        {
            this.lbCode.Text = new MxWeiXinPF.BLL.user_rate().GetCode(id);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            MxWeiXinPF.BLL.user_rate _rate = new MxWeiXinPF.BLL.user_rate();
            string code = _rate.GetCode(0);
            if (!string.IsNullOrEmpty(code))
            {
                this.lbCode.Text = code;
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"您已有兑换码尚未完成兑换，不能重复提交\");</script>");
            }
            else if (!this.DoAdd())
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"获取兑换码出错\");</script>");
            }
            else
            {
                this.BindCode(0);
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"获取兑换码成功\");</script>");
            }
        }

        private bool DoAdd()
        {
            bool flag = false;
            MxWeiXinPF.BLL.user_rate _rate = new MxWeiXinPF.BLL.user_rate();
            if (_rate.AddPoints(base.wid,base.shopid, base.openid, this.uid, "0", "3", "积分兑换") > 0)
            {
                flag = true;
            }
            return flag;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                base.wid = MyCommFun.RequestInt("wid");
                base.openid = MyCommFun.RequestOpenid();
                this.shopid = MyCommFun.RequestInt("shopid");
                this.indexurl = string.Concat(new object[] { "index.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                this.carturl = string.Concat(new object[] { "cart.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                this.userinfourl = string.Concat(new object[] { "userinfo.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                this.categoryurl = MyCommFun.urlAddOpenid("/shop/category.aspx?wid=" + base.wid + "&shopid=" + shopid, base.openid);
                MxWeiXinPF.Model.users modelByOpenid = new MxWeiXinPF.Model.users();
                MxWeiXinPF.BLL.users users2 = new MxWeiXinPF.BLL.users();
                if (HttpContext.Current.Session["dt_session_user_info"] != null)
                {
                    this.user_info = HttpContext.Current.Session["dt_session_user_info"] as MxWeiXinPF.Model.users;
                    this.uid = this.user_info.id;
                }
                else
                {
                    modelByOpenid = users2.GetModelByOpenid(base.openid);
                    this.uid = modelByOpenid.id;
                }
                this.RptBind(base.wid, base.openid);
                if (!this.Page.IsPostBack)
                {

                    this.BindCode(0);
                }
            }
            catch (Exception ex)
            {

            }

        }

        private void RptBind(int wid, string openid)
        {
            DataSet dateSetByWidOpenID = new MxWeiXinPF.BLL.user_rate().GetDateSetByWidOpenID(openid);
            if ((dateSetByWidOpenID != null) && (dateSetByWidOpenID.Tables[0].Rows.Count > 0))
            {
                if ((dateSetByWidOpenID.Tables[0].Rows[0]["hisRate"].ToString() != null) && (dateSetByWidOpenID.Tables[0].Rows[0]["hisRate"].ToString() != ""))
                {
                    this.historyRate = dateSetByWidOpenID.Tables[0].Rows[0]["hisRate"].ToString();
                }
                if ((dateSetByWidOpenID.Tables[0].Rows[0]["thisRate"].ToString() != null) && (dateSetByWidOpenID.Tables[0].Rows[0]["thisRate"].ToString() != ""))
                {
                    this.thisRate = dateSetByWidOpenID.Tables[0].Rows[0]["thisRate"].ToString();
                }
                if ((dateSetByWidOpenID.Tables[0].Rows[0]["yjsRate"].ToString() != null) && (dateSetByWidOpenID.Tables[0].Rows[0]["yjsRate"].ToString() != ""))
                {
                    this.yjsRate = dateSetByWidOpenID.Tables[0].Rows[0]["yjsRate"].ToString();
                }
            }
        }
    }
}